<script>
	import utils from './common/utils.js'
	export default {
		onLaunch: function(e) {
			//console.log('App Launch');
			let _this = this;
			// if (uni.getStorageSync('token')) {
			// 	let classify = uni.getStorageSync('classify');
			// 	//console.log(uni.getStorageSync('token'));
			// 	uni.request({
			// 		url: _this.config.app_url + '/index.php/api/user.index/detail',
			// 		data: {
			// 			app_id: 10001
			// 		},
			// 		success: (result) => {
			// 			//console.log('onLaunch - classify : ' + classify);
			// 			if (result.code == -1) {
			
			// 			} else {
			// 				if (classify == 2) {
			// 					uni.navigateTo({
			// 						url: '/pagesUser/home/home'
			// 					});
			// 				} else if (classify == 1) {
			// 					uni.navigateTo({
			// 						url: '/pages/home/home'
			// 					});
			// 				}
			// 			}
			// 		}
			// 	})
			
			// }
			//#ifdef MP-WEIXIN	
			//检查更新
			this.updateManager();
			wx.login(); //重新登录,防止解密失败
			//#endif
			// #ifdef APP-PLUS
			plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
				uni.request({
					url: _this.config.app_url + '/index.php/api/index/update',
					data: {
						version: widgetInfo.version,
						name: widgetInfo.name,
						app_id: _this.config.app_id,
						platform: uni.getSystemInfoSync().platform
					},
					success: (result) => {
						var data = result.data.data.result;

						if (data.update && data.wgtUrl) {
							uni.showModal({
								title: "发现新版本",
								content: "确认下载更新",
								success: (res) => {
									if (res.confirm == true) { //当用户确定更新，执行更新
										uni.showLoading({
									        title: '更新中……'
									    })
										 uni.downloadFile({
										 	url: data.wgtUrl,
										 	success: (downloadResult) => {
												uni.hideLoading();
										 		if (downloadResult.statusCode === 200) {
										 			plus.runtime.install(downloadResult.tempFilePath, {
										 				force: false
										 			}, function() {
										 				console.log('install success...');
										 				plus.runtime.restart();
										 			}, function(e) {
										 				console.error('install fail...');
										 			});
										 		}
										 	}
										 });
									}
								}
							})							
						}
					}
				});
			});
			// #endif
			//应用启动参数
			this.onStartupScene(e.query);
			this.getCommonConfig();
			// this.getTabBarLinks();
		},
		onShow: function() {
			//console.log('App Show')
			//#ifdef APP-PLUS
			getApp().globalData.vueObj = this;
			//#endif
		},
		onHide: function() {
			//console.log('App Hide')
		},
		methods: {
			updateManager: function() {
				const updateManager = uni.getUpdateManager();
				updateManager.onCheckForUpdate(function(res) {
					// 请求完新版本信息的回调
					if (res.hasUpdate) {
						updateManager.onUpdateReady(function(res2) {
							uni.showModal({
								title: '更新提示',
								content: '新版本已经准备好，即将重启应用',
								showCancel: false,
								success(res2) {
									if (res2.confirm) {
										// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
										updateManager.applyUpdate();
									}
								}
							});
						});
					}
				});

				updateManager.onUpdateFailed(function(res) {
					// 新的版本下载失败
					uni.showModal({
						title: '更新提示',
						content: '检查到有新版本，但下载失败，请检查网络设置',
						showCancel: false
					});
				});
			},
			/**
			 * 小程序启动场景
			 */
			onStartupScene(query) {
				// 获取场景值
				let scene = utils.getSceneData(query);
				// 记录推荐人id
				let refereeId = query.referee_id;
				if (refereeId > 0) {
					if (!uni.getStorageSync('referee_id')) {
						uni.setStorageSync('referee_id', refereeId);
					}
				}
				// 记录分销人id
				let uid = scene.uid;
				if (uid > 0) {
					uni.setStorageSync('referee_id', uid);
				}
			},
			getTabBarLinks() {
				uni.request({
					url: this.config.app_url + '/index.php/api/index/nav',
					data: {
						app_id: this.config.app_id
					},
					success: (result) => {
						uni.setStorageSync('TabBar', result.data.data);
						uni.setStorageSync('TabBarUser', result.data.data);
					}
				});
			},
			getCommonConfig() {
				//通用设置
				uni.request({
					url: this.config.app_url + '/index.php/api/index/config',
					data: {
						app_id: this.config.app_id
					},
					success: (result) => {
						uni.setStorageSync('commonConfig', result.data.data);
					}
				});
			}
		}
	}
</script>

<style>
	/* #ifndef APP-NVUE */
	@import './common/iconfont.css';
	/*每个页面公共css */
	
	@import './common/style.css';
	/* #endif */
</style>
